import { useState } from "react";
import useProfileInfoAction from "./useProfileInfoAction";
import { IChannelCategoriesItem } from "@/typing";
import Taro from "@tarojs/taro";
import { STORE_KEY } from "@/common/config";




const useJobTypeAction=()=>{
        const [channelCategoriesList, setChannelCategoriesList] = useState<Array<IChannelCategoriesItem>>([])
    
    const {  handleInitJobInfo } = useProfileInfoAction()

    const handleInitJobListT = async () => {
        const list = await handleInitJobInfo();
        setChannelCategoriesList(list)


    }
    const handleInitJobDescT= (jobIdList:Array<number>)=>{
       
 Taro.setStorageSync(STORE_KEY.JOB_ID_LIST, JSON.stringify(jobIdList));
        const selectedJobNames = jobIdList.map(id => {
            const selectedJob = channelCategoriesList.flatMap(item => item.jobType).find(job => job.id === id);
            return selectedJob ? selectedJob.name : null;
        }).filter(name => name !== null); // Filter out any null values

        Taro.setStorageSync(STORE_KEY.JOB_ID_DESC, JSON.stringify(selectedJobNames));
        const storeJobDesc = selectedJobNames.join('、')
        return storeJobDesc
    }
    const handleSubmitJobType = async (jobIdList:Array<number>)=>{
        const computedJobObj = {}
        jobIdList.forEach((id, index) => {

            const keyIdVal = `job_type_id[${[index]}]`
            computedJobObj[keyIdVal] = id


        })
        return computedJobObj
    }
    return {
        handleInitJobListT,
        handleInitJobDescT,
        setChannelCategoriesList,
        handleSubmitJobType,
      
        channelCategoriesList
    }
}
export default useJobTypeAction